package com.xcy.prime;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Html;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

public class ValentineActivity extends AppCompatActivity {


    private TextView timerTextView;
    private TextView quta_tv;
    private Runnable runnable;
    private Date startDate;
    private Button backBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_valentine);

        backBtn = findViewById(R.id.backBtn2);

        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(ValentineActivity.this, MainActivity.class);
                startActivity(intent);

            }
        });


// 获取给定时间的时间戳
        long givenTime = ZonedDateTime.of(2019, 6, 29, 17, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli();

        // 定义界面上显示时间间隔的 TextView
        TextView timeIntervalTextView = findViewById(R.id.timeIntervalTextView);

        // 创建一个 Handler 对象，用于在主线程更新界面上的时间间隔
        Handler handler = new Handler(Looper.getMainLooper());

        // 创建一个 Runnable 对象，用于更新时间间隔
        Runnable updateTimeInterval = new Runnable() {
            @Override
            public void run() {
                // 获取当前时间的时间戳
                long currentTime = System.currentTimeMillis();

                // 计算时间间隔
                long interval = currentTime - givenTime;

                // 将时间间隔转换为 x年x月x日x分x秒 的形式
                long seconds = interval / 1000;
                long minutes = seconds / 60;
                long hours = minutes / 60;
                long days = hours / 24;
                long months = days / 30;
                long years = months / 12;

                // 构造时间间隔的字符串
                String timeIntervalString = "我们已相恋" + years + "年" + (months % 12) + "月" + (days % 30) + "日" + (hours % 24) + "小时" + (minutes % 60) + "分" + (seconds % 60) + "秒";

                // 在主线程更新界面上的时间间隔
                timeIntervalTextView.setText(timeIntervalString);

                // 每隔一秒钟更新一次时间间隔
                handler.postDelayed(this, 1000);
            }
        };

// 开始更新时间间隔
        handler.post(updateTimeInterval);


    }


}